/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.da;

import com.stip.server.entities.Activeties;
import com.stip.server.entities.Locations;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
public class LocationDA {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 @PersistenceContext 
 EntityManager em;
 public Locations findlocaLocationsByid(Integer id){
 Locations locations=em.find(Locations.class, id);
 return  locations;
 }
 public Integer createLocations(String name,Double locationX,Double locationY){
 Locations locations=new Locations();
 locations.setLocationName(name);
 locations.setLocationX(locationX);
 locations.setLocationY(locationY);
 em.persist(locations);
 em.flush();
 List<Activeties> list=new ArrayList<Activeties>();
 Query query=em.createQuery("SELECT a FROM Activeties a WHERE a.location = :location");
 query.setParameter("location", locations);
 List list1=query.getResultList();
     for (Iterator it = list1.iterator(); it.hasNext();) {
         Activeties object = (Activeties)it.next();
         list.add(object);
     }
 locations.setActivetiesList(list1);
 em.merge(locations);
 return  locations.getId();
 }
 public Boolean eidtLocations(Integer id,String name,Double locationX,Double locationY){
 Locations locations=em.find(Locations.class, id);
     if (locations!=null) {
         if (locationX==null) {
             locations.setLocationX(locations.getLocationX());
         } else {
             locations.setLocationX(locationX);
         }
         if (name==null) {
             locations.setLocationName(locations.getLocationName());
         } else {
             locations.setLocationName(name);
         }
         if (locationY==null) {
             locations.setLocationY(locations.getLocationY());
         } else {
             locations.setLocationY(locationY);
         }
         em.merge(locations);
         
         
         return  true;
     } else {
         return false;
     }
 }
 public Boolean removeLocations(Integer id){
 Locations locations=em.find(Locations.class, id);
     if (locations!=null) {
         em.remove(locations);
         return  true;
     } else {
         return  false;
     }
 }
}
